#include "SQLPivot.h"
#include "../../sql/visitor/SQLASTVisitor.h"
#include "../../utils/listTran.h"
// #include "statement/SQLSelectItem.h"

SQLPivot::SQLPivot()
{
  items = std::make_shared<std::list<SQLSelectItem_ptr>>();
  pivotFor = std::make_shared<std::list<SQLExpr_ptr>>();
  pivotIn = std::make_shared<std::list<SQLSelectItem_ptr>>();
}

SQLExpr_list_ptr SQLPivot::getPivotFor()
{
  return pivotFor;
}

SQLSelectItem_list_ptr SQLPivot::getPivotIn()
{
  return pivotIn;
}

void SQLPivot::accept0(SQLASTVisitor_ptr v)
{
  if (v->visit(SharedObject(SQLPivot)))
  {
    SQLObject_list_ptr tmp1 = ListTran::tran_class<SQLSelectItem, SQLObject>(this->items);
    acceptChild(v, tmp1);
    SQLObject_list_ptr tmp2 = ListTran::tran_class<SQLExpr, SQLObject>(this->pivotFor);
    acceptChild(v, tmp2);
    SQLObject_list_ptr tmp3 = ListTran::tran_class<SQLSelectItem, SQLObject>(this->pivotIn);
    acceptChild(v, tmp3);
  }
  v->endVisit(SharedObject(SQLPivot));
}

SQLSelectItem_list_ptr SQLPivot::getItems()
{
  return items;
}

void SQLPivot::addItem(SQLSelectItem_ptr item)
{
  if (item != NULL)
  {
    item->setParent(SharedObject(SQLPivot));
  }
  this->items->push_back(item);
}

BOOL_ptr SQLPivot::isXml()
{
  return xml;
}

void SQLPivot::setXml(BOOL_ptr xml)
{
  this->xml = xml;
}

SQLObject_ptr SQLPivot::clone()
{
  SQLObject_ptr clone_tmp = std::shared_ptr<SQLPivot>(new SQLPivot());
  std::shared_ptr<SQLPivot> x = std::dynamic_pointer_cast<SQLPivot>(clone_tmp);
  // SQLPivot *x = new SQLPivot();

  x->setXml(this->xml);

  for (SQLSelectItem_ptr e : *this->items)
  {
    SQLSelectItem_ptr e2 = e;
    e2->setParent(x);
    x->getItems()->push_back(e2);
  }

  for (SQLExpr_ptr e : *this->pivotFor)
  {
    SQLExpr_ptr e2 = e;
    e2->setParent(x);
    x->getPivotFor()->push_back(e2);
  }

  for (SQLSelectItem_ptr e : *this->pivotIn)
  {
    SQLSelectItem_ptr e2 = e;
    e2->setParent(x);
    x->getPivotIn()->push_back(e2);
  }

  return x;
}
